import 'package:bitcoin_mine/common/global.dart';
import 'package:bitcoin_mine/model/mine_info.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../l10n/app_localizations.dart';

class BtcUsdChart extends StatelessWidget {
  const BtcUsdChart({super.key});

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context);

    return Container(
      margin: EdgeInsets.only(top: 24.h),
      padding: EdgeInsets.all(16.w),
      decoration: BoxDecoration(
        color: const Color(0xFFFFFFFF),
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 图表标题
          Text(
            l10n.btcUsd,
            style: TextStyle(
              color: Colors.black,
              fontSize: 16.sp,
              fontWeight: FontWeight.w600,
            ),
          ),

          SizedBox(height: 16.h),
          // Image.asset("assets/images/pic_btc_chart.png", fit: BoxFit.fill),
          // 图表区域
          SizedBox(
            height: 120.h,
            child: Obx(() {
              final data = Global.mineInfo.value?.bitcoin_history.data ?? [];
              return CustomPaint(
                painter: ChartPainter(data),
                size: Size.infinite,
              );
            }),
          ),
        ],
      ),
    );
  }
}

class ChartPainter extends CustomPainter {
  final List<Data> data;

  ChartPainter(this.data);

  @override
  void paint(Canvas canvas, Size size) {
    if (data.isEmpty || data.length < 2) return;

    final paint =
        Paint()
          ..color = const Color(0xFF0062FD)
          ..strokeWidth = 2.0
          ..style = PaintingStyle.stroke;

    final fillPaint =
        Paint()
          ..color = const Color(0xFF0062FD).withOpacity(0.2)
          ..style = PaintingStyle.fill;

    final path = Path();
    final fillPath = Path();

    // 计算数据点
    final points = <Offset>[];
    final maxY = data
        .map((d) => double.parse(d.priceUsd))
        .reduce((a, b) => a > b ? a : b);
    final minY = data
        .map((d) => double.parse(d.priceUsd))
        .reduce((a, b) => a < b ? a : b);
    final yRange = maxY - minY;

    for (int i = 0; i < data.length; i++) {
      final x = (i / (data.length - 1)) * size.width;
      final y =
          size.height -
          ((double.parse(data[i].priceUsd) - minY) / yRange) * size.height;
      points.add(Offset(x, y));
    }

    // 绘制填充区域
    fillPath.moveTo(points.first.dx, size.height);
    for (final point in points) {
      fillPath.lineTo(point.dx, point.dy);
    }
    fillPath.lineTo(points.last.dx, size.height);
    fillPath.close();
    canvas.drawPath(fillPath, fillPaint);

    // 绘制线条
    path.moveTo(points.first.dx, points.first.dy);
    for (int i = 1; i < points.length; i++) {
      path.lineTo(points[i].dx, points[i].dy);
    }
    canvas.drawPath(path, paint);

    // 绘制数据点
    final pointPaint =
        Paint()
          ..color = const Color(0xFF0062FD)
          ..style = PaintingStyle.fill;

    for (final point in points) {
      canvas.drawCircle(point, 3.0, pointPaint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
